package carddeck;

import java.util.LinkedList;

public class PlayerCardSet {
	private LinkedList<Card> cardSet;

	public PlayerCardSet() {
		cardSet = new LinkedList<Card>();
	}

	public void addCard(Card theCard) {
		cardSet.add(theCard);
		sortCards();
	}
	
	public void removeCard(Card card) {
		cardSet.remove(card);
	}

	public void sortCards() {
		LinkedList<Card> tempSet = new LinkedList<Card>();
		while (cardSet.size() > 0)
			tempSet.add(getLeftmost());
		cardSet = tempSet;
	}

	public LinkedList<Card> getCards() {
		return cardSet;
	}

	private Card getLeftmost() {
		int lowestValue = 100;
		int lowestIndex = 0;
		for (int i = 0; i < cardSet.size(); i++) {
			int val = cardSet.get(i).getCardRank().getValue();
			Suit cardSuit = cardSet.get(i).getCardSuit();
			if (cardSuit == Suit.HEARTS)
				;
			else if (cardSuit == Suit.CLUBS)
				val += 13;
			else if (cardSuit == Suit.DIAMONDS)
				val += 26;
			else if (cardSuit == Suit.SPADES)
				val += 39;
			else
				// Joker
				val = 0;
			if (val < lowestValue) {
				lowestIndex = i;
				lowestValue = val;
			}
			if (lowestValue == 0)
				break;
		}
		Card theCard = cardSet.get(lowestIndex);
		cardSet.remove(lowestIndex);
		return theCard;
	}

	public int getCardsValues() {
		int val = 0;
		for (Card theCard : cardSet) {
			val += theCard.getCardValue();
		}
		return val;
	}

	public String cardsToString() {
		String str = "";
		for (Card theCard : cardSet) {
			str += theCard.cardToString();
			str += ", ";
		}
		str = str.substring(0, str.length() - 2);
		return str;
	}
}
